package com.tbit.uqbike.client.controller;

import com.tbit.uqbike.client.constant.MapConstant;
import com.tbit.uqbike.client.constant.ResponseCode;
import com.tbit.uqbike.client.constant.SysConstant;
import com.tbit.uqbike.client.exception.BaseException;
import com.tbit.uqbike.client.pojo.Point;
import com.tbit.uqbike.client.pojo.Result;
import com.tbit.uqbike.client.pojo.User;
import com.tbit.uqbike.client.pojo.newEntity.Forbidden;
import com.tbit.uqbike.client.service.CalibrateService;
import com.tbit.uqbike.client.service.ForbiddenService;
import com.tbit.uqbike.client.service.TokenService;
import com.tbit.uqbike.client.util.Assert;
import com.tbit.uqbike.client.util.OutputUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 禁行区
 *
 * @Author: chen
 * @Description:
 * @Date: 创建于 9:55 2022/12/23
 */
@RequestMapping("/forbidden")
@Controller
public class ForbiddenController {

    @Autowired
    private ForbiddenService forbiddenService;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private CalibrateService calibrateService;

    /**
     * 获取附近禁行区列表
     *
     * @param token     令牌
     * @param lo        经度
     * @param la        纬度
     * @param accountId 区域id
     * @param mapType   地图类型
     */
    @RequestMapping("/getByArea")
    public Result<List<Forbidden>> getByArea(String token, Double lo, Double la, Integer accountId, Integer mapType) {
        User user = tokenService.getUser(token);

        Assert.notNull(accountId, "-10001", "区域id不能为空");
        Assert.notNull(lo, "-2001", "经度不能为空");
        Assert.notNull(la, "-2002", "纬度不能为空");

        if (mapType == null) {
            mapType = MapConstant.MAP_BAIDU;
        }

        /*校准百度经纬度为原始经纬度*/
        Point point = new Point(lo, la);
        point = calibrateService.calibrate(point, mapType, SysConstant.ORIGINAL);

        return Result.success(
                forbiddenService.getByArea(accountId, point.getX(), point.getY())
        );
    }

    /**
     * 获取设备所在禁行区
     *
     * @param userCode 车辆编号
     * @param mapType  地图类型
     * @param request
     * @param response
     */
    @RequestMapping("/getByUserCode")
    public void getByUserCode(String userCode, Integer mapType,
                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.hasLength(userCode, "-30001", "machineNO不能为空");

            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }
            List<Forbidden> forbidden = forbiddenService.getByUserCode(userCode, mapType);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, forbidden);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }

    /**
     * 获取所在禁行区
     *
     * @param accountId 区域ID
     * @param mapType   地图类型
     * @param request
     * @param response
     */
    @RequestMapping("/getByAccountId")
    public void getByUserCode(Integer accountId, Integer mapType,
                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> map = new HashMap<String, Object>();

        try {
            /* 校验 */
            Assert.notNull(accountId, "-30001", "区域id不能为空");
            if (mapType == null) {
                mapType = MapConstant.MAP_GOOGLE;
            }
            List<Forbidden> forbidden = forbiddenService.getByAccountId(accountId, mapType);
            map.put(ResponseCode.RET, ResponseCode.SUCC);
            map.put(ResponseCode.DATA, forbidden);
        } catch (BaseException e) {
            ResponseCode.printException(map, e, request);
        }

        OutputUtil.renderText(request, response, map);
    }
}
